import json
import math
from math import ceil

from apps.crawler_platform.core_platform.core_g import CoreSqlValue
from apps.crawler_platform.core_platform.g_model import DealModel, CallBackModel, JournalListModel, JournalIssueModel, \
    DealInsertModel, DealItemNextModel, DealUpdateModel, JournalHomeModel, OperatorSqlModel

__all__ = ["wanfangjournal_wanfangqkhome_callback",
           "wanfangjournal_wanfangclasshome_callback",
           "wanfangjournal_wanfangqkclasshome_callback",
           "wanfangjournal_wanfangjournallist_callback",
           "wanfangjournal_wanfangissue_callback",
           "wanfangjournal_wanfangarticle_callback"]


def wanfangjournal_wanfangjournallist_callback(callmodel: CallBackModel[JournalListModel]) -> DealModel:
    issue_json = {}
    journal_json_dicts = {}
    result = DealModel()
    para_dicts = callmodel.para_dicts
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    journal_rawid = callmodel.sql_model.journal_rawid
    d_i_model = DealInsertModel()
    d_i_model.insert_pre = CoreSqlValue.replace_it
    info_dicts = {"task_name": task_info.task_name,
                  "task_tag": task_info.task_tag,
                  "journal_rawid": journal_rawid,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    lists = para_dicts["data"]["1_1"]["perio_evaluate"]["children"]
    wf_impact = "0"
    for item in lists:
        if item["affectoi"].find("影响因子") > -1:
            wf_impact = item["affectoi"].split("：")[-1].strip()
    journal_json_dicts["wf_impact"] = wf_impact
    lists2 = para_dicts["data"]["1_2"]["synopsis_select"]["children"]
    for item in lists2:
        value = item["div_tag"].replace("\xa0", "")
        value = value.strip()
        if value.find("主编") > -1:
            major_editor = item["div_tag"].split("：")[-1].strip()
            journal_json_dicts["chief_editor"] = major_editor
    lists3 = para_dicts["data"]["1_3"]["formalPublishes"]
    for item in lists3:
        pub_year = item["name"]
        for item2 in item["perioBatches"]:
            num = item2["pn"]
            temp = info_dicts.copy()
            temp["task_tag"] = temp["task_tag_next"].split(";")[-1]
            del temp["task_tag_next"]
            temp.update({'pub_year': pub_year, 'num': num, "page": "1",
                         'issue_json': json.dumps(issue_json, ensure_ascii=False)})
            # result.next_dicts["insert_ig"]["lists"].append(temp)
            d_i_model.lists.append(temp)
    result.next_dicts.insert.append(d_i_model)
    qk_dicts = json.loads(callmodel.sql_model.journal_json)
    qk_dicts.update(journal_json_dicts)
    result.befor_dicts.update.update({"journal_json": json.dumps(qk_dicts, ensure_ascii=False)})
    return result


def wanfangjournal_wanfangissue_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    dealmodel = DealModel()
    pageTotal = callmodel.para_dicts["1_1"]["data"]["1"]["pageTotal"]
    totalRow = callmodel.para_dicts["1_1"]["data"]["1"]["totalRow"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info

    dealmodel.befor_dicts.update.update({"articlecount": totalRow, "page": pageTotal})

    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}

    pub_year = callmodel.sql_model.pub_year
    num = callmodel.sql_model.num
    journal_rawid = callmodel.sql_model.journal_rawid
    d_i_model = DealInsertModel()
    d_i_model.insert_pre = CoreSqlValue.replace_it
    for page, one_page in callmodel.para_dicts["1_1"]["data"].items():
        if one_page["pageRow"] is not None:
            for dict_item in one_page["pageRow"]:
                rawid = dict_item["Id"]
                # 有可能出现没有title的信息
                title = dict_item.get("Title", "")
                page_info = dict_item.get("Page", "")
                column_info = dict_item.get("Column", "")
                author = dict_item.get("Creator", "")

                temp = info_dicts.copy()
                temp["task_tag"] = temp["task_tag_next"]
                del temp["task_tag_next"]
                temp["rawid"] = rawid
                json_temp = {
                    "title": title,
                    "page_info": page_info,
                    "column_info": column_info,
                    "author": author,
                    "pub_year": pub_year,
                    "num": num,
                    "journal_rawid": journal_rawid
                }
                temp["article_info_json"] = json.dumps(json_temp, ensure_ascii=False)
                # dealmodel.next_dicts["replace_"]["lists"].append(temp)
                d_i_model.lists.append(temp)
    dealmodel.next_dicts.insert.append(d_i_model)
    dealmodel.code_dicts = {"1_1": {"max_page": str(pageTotal)}}
    return dealmodel


def wanfangjournal_wanfangarticle_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    dealmodel = DealModel()
    html_dicts = callmodel.para_dicts
    html_ref_total = html_dicts["1_1"]["data"]["1"]["total"]
    html_ref_page = ceil(int(html_ref_total) / 10)
    dealmodel.code_dicts.update({"1_1": {"max_page": html_ref_page}})
    if "1_2" in html_dicts.keys():
        html_cite_total = html_dicts["1_2"]["data"]["1"]["total"]
        html_cite_page = ceil(int(html_cite_total) / 10)
        dealmodel.code_dicts.update({"1_2": {"max_page": html_cite_page}})
    return dealmodel


def wanfangjournal_wanfangclasshome_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["1_1"]['clusterField']['ClassCode']['cluster']:
            temp = info_dicts.copy()
            field = item["field"]  # 哲学政法
            number = item["number"]
            name = item["name"]  # 0/B
            name = name.replace("0/", "1/")
            temp["home_rawid"] = name
            json_temp = {
                "field": field,
                "number": number
            }

            if name.count("/") > 1:
                temp["task_tag"] = temp["task_tag_next"].split(";")[-1]
                del temp["task_tag_next"]
                befor_field = json.loads(callmodel.sql_model.home_json)["field"]
                json_temp["his"] = befor_field + "->" + field
            else:
                del temp["task_tag_next"]

            temp["home_json"] = json.dumps(json_temp, ensure_ascii=False)

            d_i_model.lists.append(temp)
        result.befor_dicts.insert.append(d_i_model)
    return result


def wanfangjournal_wanfangqkhome_callback(callmodel: CallBackModel[JournalHomeModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in para_dicts["data"]:
        data = para_dicts["data"]["1_1"]
        total = data["total"]
        total_page = math.ceil(int(total) / 50)
        result.code_dicts = {
            "1_1": {"max_page": total_page}
        }
        page_index = int(callmodel.sql_model.page_index)
        turn_page = task_info.turn_page
        if (turn_page == 7 and page_index < 3) or (turn_page == 8 and page_index < 4):
            sql_dict = callmodel.sql_model.dict()
            tmp_list = [
                {
                    "key": "task_name",
                    "value": sql_dict["task_name"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "task_tag",
                    "value": sql_dict["task_tag"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "home_rawid",
                    "value": sql_dict["home_rawid"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "page_index",
                    "value": total_page,
                    "operator": ">",
                    "description": "and"
                }
            ]
            du_model_bef_1 = DealUpdateModel()
            du_model_bef_1.update.update({"is_active": "0"})
            list_op = []
            for item in tmp_list:
                op = OperatorSqlModel()
                list_op.append(op.parse_obj(item))
            du_model_bef_1.where = list_op
            result.befor_dicts.update_list.append(du_model_bef_1)
            tmp_list[-1]["operator"] = "<="
            du_model_bef_2 = DealUpdateModel()
            du_model_bef_2.update.update({"is_active": "1"})
            list_op1 = []
            for item in tmp_list:
                op = OperatorSqlModel()
                list_op1.append(op.parse_obj(item))
            du_model_bef_2.where = list_op1
            result.befor_dicts.update_list.append(du_model_bef_2)

            di_model_bef = DealInsertModel()
            di_model_bef.insert_pre = CoreSqlValue.insert_ig_it
            sql_dict.pop("id")
            sql_dict.pop("update_time")
            sql_dict.pop("create_time")
            sql_dict.pop("null_dicts")
            sql_dict.pop("err_msg")
            sql_dict.pop("other_dicts")
            sql_dict.pop("state")
            sql_dict.pop("failcount")
            for page in range(page_index, total_page):
                sql_dict["page_index"] = page
                di_model_bef.lists.append(sql_dict.copy())
            result.befor_dicts.insert.append(di_model_bef)
        di_model_next = DealInsertModel()
        di_model_next.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["value"]:
            result.befor_dicts.update.update({'page': total_page})
            journal_rawid = item["Id"]
            qk_name = item['Title'][0].strip()
            trans_title = ''
            journal_name_alt = ""
            journal_name_korea = ""
            if len(item['Title']) >= 2:
                trans_title = item['Title'][1].strip()
                lst = trans_title.split('  ')
                journal_name_alt = lst[0]
                journal_name_korea = ''
                if len(lst) == 2:
                    journal_name_korea = lst[1]
            hostunit_name = ";".join(item["Sponsor"])  # 主办单位
            dep_name = item["CompetentDepartment"].strip()  # 主管单位
            dep_name = dep_name.replace("􀪋􀪋􀪋􀪋", "")
            publish_cycle = item.get('PublishingPeriod', '').strip()  # 出版周期
            issn = item.get('ISSN', '').strip()
            cn = item.get('CN', '').strip()
            src_db = ''
            try:
                src_db = ";".join(item['SourceDB'])
            except:
                pass
            languages = item['Language']
            languages2 = []
            for lang in languages:
                if lang == "chi":
                    languages2.append("中文")
                elif lang == "eng":
                    languages2.append("英文")
                else:
                    languages2.append(lang)
            language = ";".join(languages2)

            ImpactFactor = item.get('ImpactFactor', '')  # 影响因子
            # major_editor = dic.get('major_editor', '').strip()  # 主编

            former_name = ";".join(item["FormerTitle"])  # 曾用刊名
            perio_desc = item.get('Introduction', '').strip()  # 期刊简介

            Award = ";".join(item.get('Award', ''))  # 获奖情况
            Address = item.get('Address', '').strip()  # 地址
            Postcode = item.get('Postcode', '').strip()  # 邮政编码
            Url = item.get('Url', '').strip()  # 刊Url
            Telephone = item.get('Telephone', '').strip()  # 电话
            Email = item.get('Email', '').strip()  # Email
            LastYear = str(item.get('LastYear', ''))  # 最新年
            LastIssue = item.get('LastIssue', '').strip()  # 最终期
            Editorial = item.get('Editorial', '').strip()  # 编辑部
            SponsorRegion = item.get('SponsorRegion', '').strip()  # 主办单位城市
            Director = item.get('Director', '').strip()  # 主管
            ChiefEditor = item.get('ChiefEditor', '').strip()  # 主编
            IssuedPeriod = item.get('IssuedPeriod', '').strip()  # 出版周期
            IsStopped = item.get('IsStopped', '')  # 是否停刊
            if IsStopped:
                IsStopped = '1'
            else:
                IsStopped = '0'

            FoundYear = str(item.get('FoundYear', ''))  # 创刊年
            Fax = item.get('Fax', '').strip()  # 传真
            new_dict = {}

            new_dict["journal_name_alt"] = journal_name_alt
            new_dict["journal_name_korea"] = journal_name_korea
            new_dict["qk_name"] = qk_name
            new_dict["trans_title"] = trans_title
            new_dict["hostunit_name"] = hostunit_name
            new_dict["dep_name"] = dep_name
            new_dict["publish_cycle"] = publish_cycle
            new_dict["issn"] = issn
            new_dict["cn"] = cn
            new_dict["src_db"] = src_db
            new_dict["language"] = language
            new_dict["former_name"] = former_name
            new_dict["perio_desc"] = perio_desc
            new_dict["wf_impact"] = ImpactFactor
            new_dict["post_code"] = Postcode
            new_dict["type_name"] = IssuedPeriod
            new_dict["chief_editor"] = ChiefEditor
            new_dict["is_stop"] = IsStopped
            new_dict["email"] = Email
            new_dict["edit_office_addr"] = Address
            new_dict["tel_code"] = Telephone
            new_dict["web_site"] = Url
            new_dict["fax"] = Fax
            new_dict["award_state"] = Award
            new_dict["last_year"] = LastYear
            new_dict["LastIssue"] = LastIssue
            new_dict["Editorial"] = Editorial
            new_dict["SponsorRegion"] = SponsorRegion
            new_dict["Director"] = Director
            new_dict["FoundYear"] = FoundYear

            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["journal_rawid"] = journal_rawid
            temp_info["sub_db_id"] = "00004"
            temp_info["is_active"] = "1"
            temp_info["journal_json"] = json.dumps(new_dict, ensure_ascii=False)
            di_model_next.lists.append(temp_info)
            du_model = DealUpdateModel()
            du_model.update.update({"journal_json": json.dumps(new_dict, ensure_ascii=False),
                                    "sub_db_id": "00004",
                                    "is_active": "1"})
            du_model.where.update({"journal_rawid": journal_rawid,
                                   "task_tag": temp_info["task_tag"],
                                   "task_name": callmodel.sql_model.task_name,
                                   "is_active": "0"})

            result.next_dicts.update_list.append(du_model)
        result.next_dicts.insert.append(di_model_next)
    return result


def wanfangjournal_wanfangqkclasshome_callback(callmodel: CallBackModel[JournalIssueModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    home_json = json.loads(callmodel.sql_model.home_json)
    his = home_json["his"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "sub_db_id": callmodel.sql_model.sub_db_id,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in para_dicts["data"]:
        data = para_dicts["data"]["1_1"]
        total = data["total"]
        total_page = math.ceil(int(total) / 50)
        result.code_dicts = {
            "1_1": {"max_page": total_page}
        }
        page_index = int(callmodel.sql_model.page_index)
        turn_page = task_info.turn_page
        if (turn_page == 7 and page_index < 3) or (turn_page == 8 and page_index < 4):
            sql_dict = callmodel.sql_model.dict()
            tmp_list = [
                {
                    "key": "task_name",
                    "value": sql_dict["task_name"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "task_tag",
                    "value": sql_dict["task_tag"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "home_rawid",
                    "value": sql_dict["home_rawid"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "page_index",
                    "value": total_page,
                    "operator": ">",
                    "description": "and"
                }
            ]
            du_model_bef_1 = DealUpdateModel()
            du_model_bef_1.update.update({"is_active": "0"})
            list_op = []
            for item in tmp_list:
                op = OperatorSqlModel()
                list_op.append(op.parse_obj(item))
            du_model_bef_1.where = list_op
            result.befor_dicts.update_list.append(du_model_bef_1)
            tmp_list[-1]["operator"] = "<="
            du_model_bef_2 = DealUpdateModel()
            du_model_bef_2.update.update({"is_active": "1"})
            list_op1 = []
            for item in tmp_list:
                op = OperatorSqlModel()
                list_op1.append(op.parse_obj(item))
            du_model_bef_2.where = list_op1
            result.befor_dicts.update_list.append(du_model_bef_2)

            di_model_bef = DealInsertModel()
            di_model_bef.insert_pre = CoreSqlValue.insert_ig_it
            sql_dict.pop("id")
            sql_dict.pop("update_time")
            sql_dict.pop("create_time")
            sql_dict.pop("null_dicts")
            sql_dict.pop("err_msg")
            sql_dict.pop("other_dicts")
            sql_dict.pop("state")
            sql_dict.pop("failcount")
            for page in range(page_index, total_page):
                sql_dict["page_index"] = page
                di_model_bef.lists.append(sql_dict.copy())
            result.befor_dicts.insert.append(di_model_bef)
        di_model_next = DealInsertModel()
        di_model_next.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["value"]:
            result.befor_dicts.update.update({'page': total_page})
            journal_rawid = item["Id"]
            qk_name = item['Title'][0].strip()
            trans_title = ''
            journal_name_alt = ""
            journal_name_korea = ""
            if len(item['Title']) >= 2:
                trans_title = item['Title'][1].strip()
                lst = trans_title.split('  ')
                journal_name_alt = lst[0]
                journal_name_korea = ''
                if len(lst) == 2:
                    journal_name_korea = lst[1]
            hostunit_name = ";".join(item["Sponsor"])  # 主办单位
            dep_name = item["CompetentDepartment"].strip()  # 主管单位
            dep_name = dep_name.replace("􀪋􀪋􀪋􀪋", "")
            publish_cycle = item.get('PublishingPeriod', '').strip()  # 出版周期
            issn = item.get('ISSN', '').strip()
            cn = item.get('CN', '').strip()
            src_db = ''
            try:
                src_db = ";".join(item['SourceDB'])
            except:
                pass
            languages = item['Language']
            languages2 = []
            for lang in languages:
                if lang == "chi":
                    languages2.append("中文")
                elif lang == "eng":
                    languages2.append("英文")
                else:
                    languages2.append(lang)
            language = ";".join(languages2)

            ImpactFactor = item.get('ImpactFactor', '')  # 影响因子
            # major_editor = dic.get('major_editor', '').strip()  # 主编

            former_name = ";".join(item["FormerTitle"])  # 曾用刊名
            perio_desc = item.get('Introduction', '').strip()  # 期刊简介

            Award = ";".join(item.get('Award', ''))  # 获奖情况
            Address = item.get('Address', '').strip()  # 地址
            Postcode = item.get('Postcode', '').strip()  # 邮政编码
            Url = item.get('Url', '').strip()  # 刊Url
            Telephone = item.get('Telephone', '').strip()  # 电话
            Email = item.get('Email', '').strip()  # Email
            LastYear = str(item.get('LastYear', ''))  # 最新年
            LastIssue = item.get('LastIssue', '').strip()  # 最终期
            Editorial = item.get('Editorial', '').strip()  # 编辑部
            SponsorRegion = item.get('SponsorRegion', '').strip()  # 主办单位城市
            Director = item.get('Director', '').strip()  # 主管
            ChiefEditor = item.get('ChiefEditor', '').strip()  # 主编
            IssuedPeriod = item.get('IssuedPeriod', '').strip()  # 出版周期
            IsStopped = item.get('IsStopped', '')  # 是否停刊
            if IsStopped:
                IsStopped = '1'
            else:
                IsStopped = '0'

            FoundYear = str(item.get('FoundYear', ''))  # 创刊年
            Fax = item.get('Fax', '').strip()  # 传真
            new_dict = {}

            new_dict["journal_name_alt"] = journal_name_alt
            new_dict["journal_name_korea"] = journal_name_korea
            new_dict["qk_name"] = qk_name
            new_dict["trans_title"] = trans_title
            new_dict["hostunit_name"] = hostunit_name
            new_dict["dep_name"] = dep_name
            new_dict["publish_cycle"] = publish_cycle
            new_dict["issn"] = issn
            new_dict["cn"] = cn
            new_dict["src_db"] = src_db
            new_dict["language"] = language
            new_dict["former_name"] = former_name
            new_dict["perio_desc"] = perio_desc
            new_dict["wf_impact"] = ImpactFactor
            new_dict["post_code"] = Postcode
            new_dict["type_name"] = IssuedPeriod
            new_dict["chief_editor"] = ChiefEditor
            new_dict["is_stop"] = IsStopped
            new_dict["email"] = Email
            new_dict["edit_office_addr"] = Address
            new_dict["tel_code"] = Telephone
            new_dict["web_site"] = Url
            new_dict["fax"] = Fax
            new_dict["award_state"] = Award
            new_dict["last_year"] = LastYear
            new_dict["LastIssue"] = LastIssue
            new_dict["Editorial"] = Editorial
            new_dict["SponsorRegion"] = SponsorRegion
            new_dict["Director"] = Director
            new_dict["FoundYear"] = FoundYear

            temp_info = info_dicts.copy()
            task_tag_next = temp_info["task_tag_next"]
            temp_info["task_tag"] = task_tag_next
            del temp_info["task_tag_next"]
            temp_info["journal_rawid"] = journal_rawid
            temp_info["sub_db_id"] = "00004"
            temp_info["is_active"] = "1"
            temp_info["journal_json"] = json.dumps(new_dict, ensure_ascii=False)
            di_model_next.lists.append(temp_info)

            du_model = DealUpdateModel()
            du_model.update_no_placeholder.update({"subject": f'CONCAT(`subject`,";{his}")'})
            du_model.update.update({"journal_json": json.dumps(new_dict, ensure_ascii=False),
                                    "sub_db_id": "00004",
                                    "is_active": "1"})
            du_model.where.update({"journal_rawid": journal_rawid,
                                   "task_tag": temp_info["task_tag"],
                                   "task_name": callmodel.sql_model.task_name})

            result.next_dicts.update_list.append(du_model)
        result.next_dicts.insert.append(di_model_next)
    return result
